<?php
    session_start();
    $username = @$_SESSION['username'];
    if (!$username) {
        $username = "未登录";
        
    }




    // 创建 mysql连接
    $con = @mysqli_connect("localhost","root","123456","news",3306);
    // 判断 mysql 连接
    if (mysqli_connect_errno()){
    die("连接MySQL失败:".mysqli_connect_errno()." 错误为:".mysqli_connect_error());
    }
    // 设置字符集
    mysqli_set_charset($con,"utf8");
    // 定义sql语句
    $sql = "SELECT news.id,news.title,news.content,news.buildtime,admin.name FROM news,admin WHERE news.aid = admin.id";
    // 执行sql语句
    $result = mysqli_query($con,$sql);
    if(!$result){
    die("SQL语句失败:".mysqli_errno($con)." 错误为:".mysqli_error($con));
    }
    // 定义一个数组
    $result_list = [];
    // 遍历结果集
    while($row = mysqli_fetch_assoc($result )){
    array_push($result_list,$row);
    }
    // 显示数据
 $result_list = json_encode($result_list);
    // 释放 mysql连接
    mysqli_close($con);

?>





<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>新闻列表</title>
    <style>
        body {
            background-image: url('/dom/img/img/pexels-photo-346529.webp'); /* 设置页面背景图片 */
            background-size: cover; /* 背景图片覆盖整个元素 */
            background-repeat: no-repeat; /* 背景图片不重复 */
            background-position: center center; /* 背景图片居中显示 */
            background-attachment: fixed; /* 背景图片固定，不随滚动条滚动 */
            color: #333;
            margin: 0;
            padding: 20px;
        }
        .article-container {
            background-color: #fff;
            padding: 20px;
            margin: 20px auto;
            max-width: 800px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
        }
        .article-header {
            border-bottom: 1px solid #eee;
            padding-bottom: 10px;
            margin-bottom: 15px;
        }
        .article-title {
            font-size: 24px;
            margin: 0;
        }
        .article-meta {
            font-size: 14px;
            color: #666;
        }
        .article-content {
            line-height: 1.6;
            margin-top: 20px;
        }

    </style>
</head>

        <div style="float: right; margin:30px 375px 0px 0px;"><?=$username?></div>

</body>
<script>
    let news_list = <?=$result_list?>;
    let tbody = document.querySelector("body")
    let need_insert_str = ""
    for (let item of news_list){
        need_insert_str += `
        <div class="article-container">
        <div class="article-header">
                <h1 class="article-title"><a href="./list.php?id=${item.id}">${item.title}</a></h1>
        <div class="article-meta">
                <span>作者: ${item.name}</span>
                <span> | </span>
                <span>${item.buildtime}</span>
                <span> | </span>
                <span>ID: ${item.id}</span>
        </div>
        </div>
        <div class="article-content">
            <p><a href="">${item.content}</a></p>
        </div>
        </div>
        </div>
        `
    }
    tbody.innerHTML += need_insert_str

</script>
</html>